<template>
  <div class="sys-main" style="background:#eee;">
    <!-- 登陆 -->
    <div class="container-box">
      <div class="main1">
        <div class="bg"></div>
        <div class="con">
          <img src="@/assets/sys/images/logo.png" alt="" class="logo" />
          <div class="title">
            松下认定维修店技术支持系统
          </div>
          <!-- <div class="tx"><i class="iconfont icon-lianxiren"></i></div> -->
          <el-form ref="loginForm" :model="loginForm" :rules="loginRules">
            <ul>
              <li>
                <el-input
                  type="text"
                  name="userName"
                  id="userName"
                  placeholder="用户名"
                  v-model="loginForm.username"
                />
              </li>
              <li>
                <el-input
                  name="userPass"
                  type="password"
                  id="userPass"
                  placeholder="密码"
                  v-model="loginForm.password"
                />
              </li>
              <li class="clearfix">
                <div class="list pull-left">
                  <el-input
                    type="text"
                    name="userCode"
                    id="userCode"
                    placeholder="验证码"
                    v-model="loginForm.code"
                  />
                </div>
                <a
                  class="pull-right"
                  id="yzm_con"
                  title="点击刷新"
                  @click="getCode"
                >
                  <i class="iconfont icon-shuaxin1"></i>
                  <img
                    :src="codeUrl"
                    height="16"
                    width="50"
                    style="cursor : pointer;width: 138px;height: 52px;max-height: initial;"
                    align="absmiddle"
                    class="login-code-img"
                    alt="验证码,看不清楚?请点击刷新验证码"
                  />
                </a>
              </li>

              <li @click="handleLogin">
                <a class="but" id="loginsubmit">登陆</a>
              </li>
              <li @click="onGotoForget"><a href="javascript:;" id="GetpassSubmit">忘记密码</a></li>
            </ul>
            <p class="jsonmsg" style="color: red;">{{ successText }}</p>
            <!--警告弹窗-->
            <div id="bd_msg"></div>
          </el-form>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { getCodeImg } from "@/api/login";
import Cookies from "js-cookie";
import { encrypt, decrypt } from "@/utils/jsencrypt";

export default {
  name: "Login",
  data() {
    return {
      codeUrl: "",
      cookiePassword: "",
      loginForm: {
        username: "",
        password: "",
        rememberMe: false,
        code: "",
        uuid: "",
        type: window._sysConfig.type
      },
      loginRules: {
        username: [
          { required: true, trigger: "blur", message: "用户名不能为空" }
        ],
        password: [
          { required: true, trigger: "blur", message: "密码不能为空" }
        ],
        code: [{ required: true, trigger: "change", message: "验证码不能为空" }]
      },
      loading: false,
      redirect: undefined,
      successText: ""
    };
  },
  watch: {
    $route: {
      handler: function(route) {
        this.redirect = route.query && route.query.redirect;
      },
      immediate: true
    }
  },
  created() {
    this.getCode();
    // this.getCookie();
  },
  methods: {
    onGotoForget(){
      this.$router.push({
        path: "/forget"
      });
    },
    getCode() {
      getCodeImg().then(res => {
        this.codeUrl = "data:image/gif;base64," + res.img;
        this.loginForm.uuid = res.uuid;
      });
    },
    getCookie() {
      const username = Cookies.get("username");
      const password = Cookies.get("password");
      const rememberMe = Cookies.get("rememberMe");
      this.loginForm = {
        ...this.loginForm,
        username: username === undefined ? this.loginForm.username : username,
        password:
          password === undefined ? this.loginForm.password : decrypt(password),
        rememberMe: rememberMe === undefined ? false : Boolean(rememberMe)
      };
    },
    handleLogin() {
      const { username, code, password } = this.loginForm;

      if (!username) {
        this.$message.error("请输入用户名");
        return false;
      }
      if (!password) {
        this.$message.error("请输入密码");
        return false;
      }
      if (!code) {
        this.$message.error("请输入验证码");
        return false;
      }
      this.loading = true;
      // if (this.loginForm.rememberMe) {
      //   Cookies.set("username", this.loginForm.username, { expires: 30 });
      //   Cookies.set("password", encrypt(this.loginForm.password), {
      //     expires: 30
      //   });
      //   Cookies.set("rememberMe", this.loginForm.rememberMe, {
      //     expires: 30
      //   });
      // } else {
      //   Cookies.remove("username");
      //   Cookies.remove("password");
      //   Cookies.remove("rememberMe");
      // }
      this.$store
        .dispatch("Login", this.loginForm)
        .then(() => {
          this.successText = "登陆成功";
          this.$router.push({ path: this.redirect || "/" }).catch(() => {});
        })
        .catch(() => {
          this.loading = false;
          this.getCode();
        });
    }
  }
};
</script>

<style rel="stylesheet/scss" lang="scss">
.login {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100%;
  background-image: url("../assets/images/login-background.jpg");
  background-size: cover;
}
.title {
  margin: 0px auto 30px auto;
  text-align: center;
  color: #707070;
  font-size: 31px;
}

.login-form {
  border-radius: 6px;
  background: #ffffff;
  width: 400px;
  padding: 25px 25px 5px 25px;
  .el-input {
    height: 38px;
    input {
      height: 38px;
    }
  }
  .input-icon {
    height: 39px;
    width: 14px;
    margin-left: 2px;
  }
}
.login-tip {
  font-size: 13px;
  text-align: center;
  color: #bfbfbf;
}
.login-code {
  width: 33%;
  height: 38px;
  float: right;
  img {
    cursor: pointer;
    vertical-align: middle;
  }
}
.el-login-footer {
  height: 40px;
  line-height: 40px;
  position: fixed;
  bottom: 0;
  width: 100%;
  text-align: center;
  color: #fff;
  font-family: Arial;
  font-size: 12px;
  letter-spacing: 1px;
}
.login-code-img {
  height: 38px;
}
</style>
